package com.ccepc.algmanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ccepc.algmanage.mapper.PlcConfigMapper;
import com.ccepc.algmanage.service.PlcConfigService;
import com.ccepc.algmanage.service.RemoteHistoryService;
import com.ccepc.common.core.util.R;
import com.ccepc.demo.api.dto.PlcConfigDto;
import com.ccepc.demo.api.entity.PlcConfig;
import com.ccepc.demo.api.entity.RemoteHistory;
import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Service
public class PlcConfigServiceImpl extends ServiceImpl<PlcConfigMapper, PlcConfig> implements PlcConfigService {
    @Resource
    private RemoteHistoryService remoteHistoryService;

    @Override
    public R readPoint(String linkedId) {
        if (linkedId == null) {
            return R.failed("设备编号为空");
        }
        PlcConfig plcConfig = this.getOne(new LambdaQueryWrapper<PlcConfig>()
                .eq(PlcConfig::getLinkedId, linkedId));
        if (plcConfig == null || plcConfig.getPlcType() == null) {
            return R.failed("当前设备未配置PLC信息");
        }
        S7PLC s7PLC = new S7PLC(EPlcType.valueOf(plcConfig.getPlcType()), plcConfig.getPlcIp());
        return R.ok(s7PLC.readBoolean(plcConfig.getPlcPoint()));
        // 模拟获取
//        return R.ok(plcConfig.getPlcStatus() != 0);
    }

    @Override
    public R changeStatus(String linkedId, String status, boolean isHand) {
        PlcConfig plcConfig = this.getOne(new LambdaQueryWrapper<PlcConfig>()
                .eq(PlcConfig::getLinkedId, linkedId));
        if (plcConfig == null || plcConfig.getPlcType() == null) {
            return R.failed("当前设备未配置PLC信息");
        }
        try {
            // isHand表示来源（true来着页面直接控制，false来自联动记录）如果是则判断发送记录
            if (plcConfig.getLdType() == 0 || isHand) {
                S7PLC s7PLC = new S7PLC(EPlcType.valueOf(plcConfig.getPlcType()), plcConfig.getPlcIp());
                s7PLC.setConnectTimeout(5);
                s7PLC.writeBoolean(plcConfig.getPlcPoint(), Boolean.parseBoolean(status));
                if (!isHand) {
                    // 自动控制来源则保存记录
                    RemoteHistory remoteHistory = new RemoteHistory();
                    remoteHistory.setLinkedId(Integer.valueOf(linkedId));
                    remoteHistory.setActPerson("系统操作");
                    remoteHistory.setActTime(LocalDateTime.now());
                    remoteHistory.setType(0);
                    remoteHistory.setStatus(1);
                    remoteHistoryService.save(remoteHistory);
                }
            } else {
                return R.failed("不符合发送条件");
            }
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }
        return R.ok();
    }

}
